package com.example.emergencyattempt01.activity.administrator_activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.LoginActivity;
import com.example.emergencyattempt01.activity.ex.AutoPager;
import com.example.emergencyattempt01.activity.ex.AutoPagerBuilder;
import com.example.emergencyattempt01.activity.student_activity.Student_Main_Structure;
import com.example.emergencyattempt01.fragement.administrator_fragment.Administrator_Checking;
import com.example.emergencyattempt01.fragement.administrator_fragment.Book_Manage_Fragment;
import com.example.emergencyattempt01.fragement.administrator_fragment.Person_Manage_Fragment;
import com.example.emergencyattempt01.user.Administrator;
import com.example.emergencyattempt01.user.User;

public class Administrator_main_structure extends AppCompatActivity {
      FrameLayout fragment;// 用于展示fragment
      RadioGroup radioGroup;// 用于承装菜单选项
      ImageView quit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.administrator_main_structure);
        quit=findViewById(R.id.administrator_main_quit);
        // 初始化用户
        initUser();
        // 设置自动播放器
        setAutoPlayer();
        // 设置fragment
        setFragment();
        // 设置退出
        setQuit();
    }

    // 以下部分用于防止中间进入到login界面，同时维持可登录次数
    @Override
    public void onBackPressed() {
        int count= getSharedPreferences("personal_information",MODE_PRIVATE).getInt("loginWithoutCheckCount",0);
        SharedPreferences.Editor editor=getSharedPreferences("personal_information",MODE_PRIVATE).edit();
        editor.putInt("loginWithoutCheckCount",count-1);
        editor.commit();// 切勿忘记更新！！
        Log.i("loginCount_main",count+"");
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);
    }

    private void setQuit() {
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences= Administrator_main_structure.this.getSharedPreferences("personal_information",MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putInt("loginWithoutCheckCount",0);
                editor.commit();
                Intent intent=new Intent(Administrator_main_structure.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initUser() {
        User user= Administrator.getInstance();
        User.initUser(Administrator_main_structure.this,user);
    }

    private void setFragment() {
        fragment=findViewById(R.id.fragment);
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        Fragment f=new Person_Manage_Fragment();
        ft.replace(R.id.fragment,f).commit();
        radioGroup=findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentManager fm=getFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                Fragment f=null;
                switch (checkedId)
                {
                    case R.id.person_manage_fragment:
                        f=new Person_Manage_Fragment();
                        ft.replace(R.id.fragment,f).commit();
                        break;
                    case R.id.administrator_checking_fragment:
                        f=new Administrator_Checking();
                        ft.replace(R.id.fragment,f).commit();
                        break;
                    case R.id.book_manage_fragment:
                        f=new Book_Manage_Fragment();
                        ft.replace(R.id.fragment,f).commit();
                        break;
                }
            }
        });

    }

    private void setAutoPlayer() {
        AutoPagerBuilder autoPagerBuilder=new AutoPagerBuilder(Administrator_main_structure.this);
        AutoPager autoPagerForMain=autoPagerBuilder.build();
        autoPagerForMain.start();
    }
}